<?php

class HomeController extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	    
	        array('allow', 
	              'actions'=>array('login'), 
	              'users'=>array('*'), // all users
	        ),
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	    /*
	        array('allow', // allow authenticated users to update/view
	              'actions'=>array('update','view'), 
	              'roles'=>array('authenticated')
	        ),
	        array('allow', // allow admins only to delete
	              'actions'=>array('delete'), 
	              'roles'=>array('admin'),
	        ),
	    */
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}
	
	public function actionIndex()
	{          
		Yii::log('testLog A');
		
		$this -> render('index',array(	
            'title1' => 'hello vh'
		));
		
		//echo "TESTING";
	}

	public function actionLogin()
	{
		if(!Yii::app() -> user -> isGuest)
		{
			$this->redirect(array('/hyip'));
			return true;
		}
	
		if(Yii::app()->request->isPostRequest)
		{
			/*			
			Yii::app()->session['user'] = "VHTEST";
			*/
			
			$user = new UserIdentity($_POST['username'], $_POST['password']);
			$user -> authenticate();
			
			if($user -> errorCode===UserIdentity::ERROR_NONE)
			{
				$duration= 3600*24; //*30; // 30 days
				Yii::app()->user->login($user, $duration);
                                
                                $invest = new Invest();
                                $summary = $invest ->getSummary();
                                Yii::app()->session['summary'] = $summary;
                                
				$this->redirect(array('/hyip'));
				return true;
			}
		}
		
		$this -> render('login',array());
	}
	
	public function actionLogout()
	{
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
	}
	
	public function actionChat()
	{
		//$random = rand();
		$milisecond = microtime(true);
		$key = md5($milisecond);
		
		$this -> render('chat',array(
			'key' => $key
		));
	}
	
	public function actionPostChat()
	{
		if(Yii::app()->request->isPostRequest)
		{
			try{
				$msg = $_POST['msg'];
				$nickname = $_POST['nickname'];
				$key = $_POST['key'];
				
				if($nickname == "") $nickname = "Unknown";
				
				$pusher=Yii::app()->pusher;
				$result = $pusher->trigger('test_channel','my_event',array('msg'=> $msg, 'nickname' => $nickname, 'key' => $key));
				
				echo "true";
			}
			catch(Exception $ex)
			{
				echo "false";
			}
		}
		else
		{
			echo "1";
		}
	}
}